package com.kimo3412.gradehub.controller;

import com.kimo3412.gradehub.entity.Grade;
import com.kimo3412.gradehub.response.ApiResponse;
import com.kimo3412.gradehub.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/grades")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @GetMapping("/listAll")
    public ResponseEntity<ApiResponse<List<Grade>>> listAllGrades() {
        List<Grade> grades = gradeService.findAllGrades();
        return ResponseEntity.ok(new ApiResponse<>(200, "Success", grades));
    }

    @GetMapping("/getByCourseId/{courseId}")
    public ResponseEntity<ApiResponse<List<Grade>>> getGradesByCourseId(@PathVariable Long courseId) {
        List<Grade> grades = gradeService.findGradesByCourseId(courseId);
        return ResponseEntity.ok(new ApiResponse<>(200, "Success", grades));
    }

    @PostMapping("/update")
    public ResponseEntity<ApiResponse<Grade>> updateGrade(@RequestBody Grade grade) {
        Grade updatedGrade = gradeService.updateGrade(grade);
        return ResponseEntity.ok(new ApiResponse<>(200, "Grade updated successfully", updatedGrade));
    }

    @GetMapping("/getByRange/{minScore}/{maxScore}")
    public ResponseEntity<ApiResponse<List<Grade>>> getGradesByRange(@PathVariable double minScore, @PathVariable double maxScore) {
        List<Grade> grades = gradeService.findGradesByScoreRange(minScore, maxScore);
        return ResponseEntity.ok(new ApiResponse<>(200, "Success", grades));
    }
}
